package com.wy.w_ui.view.text;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import java.util.List;


public class RichTextView extends AppCompatTextView {

    public RichTextView(Context context) {
        super(context);
    }

    public RichTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RichTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


    }


    /**
     * 设置内容
     * @param textBeans
     */
    public void setText(List<TextBean> textBeans){

        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();

        for (final TextBean textBean : textBeans) {

            SpannableString spannableString = new SpannableString(textBean.content);

            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(@NonNull View widget) {

                    if (textBean.textClick != null){
                        textBean.textClick.onTextClick(textBean);
                    }

                }

                @Override
                public void updateDrawState(@NonNull TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(Color.parseColor(textBean.color));
                    ds.setUnderlineText(false);
                }
            };

            spannableString.setSpan(clickableSpan, 0, textBean.content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            spannableStringBuilder.append(spannableString);
        }

        this.setText(spannableStringBuilder);

        this.setMovementMethod(new CircleMovementMethod(0xffcccccc, 0xffcccccc));

    }


    /**
     * 点击事件
     */
    public interface ITextClick{

        void onTextClick(TextBean textBean);
    }


    /**
     * 存放一些属性
     */
    public static class TextBean{
        private String content;
        private String color;
        private ITextClick textClick;


        public TextBean(String content, String color,ITextClick textClick) {
            this.content = content;
            this.color = color;
            this.textClick = textClick;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public ITextClick getTextClick() {
            return textClick;
        }

        public void setTextClick(ITextClick textClick) {
            this.textClick = textClick;
        }
    }

}
